package org.sg20.parsename;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class ParseNameOutputKey implements WritableComparable<ParseNameOutputKey> {
    private String fileName;
    private long lineOffset;
    public ParseNameOutputKey(){
        fileName = null;
        lineOffset = -1;
    }

    public ParseNameOutputKey(String fileN, long lineO){
        fileName = fileN;
        lineOffset = lineO;
    }

    @Override
    public int compareTo(ParseNameOutputKey o) {
        if(this.fileName.compareTo(o.fileName) == 0){
            if (lineOffset == o.lineOffset) return 0;
            return (lineOffset-o.lineOffset<0) ? -1:1;
        }else{
            return fileName.compareTo(o.fileName);
        }

    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeUTF(fileName);
        dataOutput.writeLong(lineOffset);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        fileName = dataInput.readUTF();
        lineOffset = dataInput.readLong();
    }

    @Override
    public String toString() {
        return fileName + " " + lineOffset;
    }
}
